package model;

import android.util.Log;

import org.json.JSONObject;

import java.io.IOException;

import base.BaseModel;
import base.BaseObserver;
import base.ResultCallBack;
import bean.CodeBean;
import bean.GuangGaoBean;
import bean.ResultBean;
import jiyun.com.yiyanmeng.http.HttpManager;
import jiyun.com.yiyanmeng.http.MySerivce;
import okhttp3.ResponseBody;
import utils.LogUtil;
import utils.RxUtil;

/**
 * Created by Windows on 2019/11/5.
 */

public class MissPassModel extends BaseModel {
    //请求修改密码接口
    public void requestFindpass(final ResultCallBack<Void> resultCallBack,String phone,String y_pass,String c_pass,String code) {
        MySerivce serivce = HttpManager.getInstance().getSerivce();
        serivce.findpass(phone,y_pass,c_pass,code).compose(RxUtil.<ResponseBody>changeThread())
                .subscribe(new BaseObserver<ResponseBody>() {
                    @Override
                    public void onSuccess(ResponseBody responseBody) {
                        try {
                            JSONObject jsonObject = new JSONObject(responseBody.string());
                            String  ret = (String) jsonObject.get("ret");
                            int code = Integer.parseInt(ret);
                            String  mas = (String) jsonObject.get("mas");
                            LogUtil.log(mas);
                            if (code==200){
                                resultCallBack.onSuccess(null);
                            }else {
                                resultCallBack.onFail(mas);
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFail(String error) {
                        resultCallBack.onFail(error);
                    }
                });
    }

    //请求验证码接口
    public void requestYanzhengma(final ResultCallBack<CodeBean> resultCallBack, String phone, String type) {
        MySerivce serivce = HttpManager.getInstance().getSerivce();
        serivce.yanzhengma(phone, type).compose(RxUtil.<ResultBean<CodeBean>>changeThread())
                .compose(RxUtil.<CodeBean>changeResult())
                .subscribe(new BaseObserver<CodeBean>() {
                    @Override
                    public void onSuccess(CodeBean codeBean) {
                        resultCallBack.onSuccess(codeBean);
                    }

                    @Override
                    public void onFail(String error) {
                        resultCallBack.onFail(error);
                    }
                });
    }
}
